package com.jzl.util;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

public class ProtoBufUtil {
	private ProtoBufUtil(){
		
	}
	
	public static <T> byte[] serializer(Class<T> clazz,T o) {  
        Schema<T> schema =  RuntimeSchema.getSchema(clazz);
        return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));  
    }  
  
    public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
        T obj = null;  
        try {  
            obj = clazz.newInstance();  
            Schema<T> schema = RuntimeSchema.getSchema(clazz);  
            ProtostuffIOUtil.mergeFrom(bytes, obj, schema);  
        } catch (InstantiationException e) {  
            e.printStackTrace();  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  
        }  
        return obj;  
    }  
}
